<?php

namespace App\Utils;

use App\Models\Admin\Framework\AdminAccessLogModel;
use App\Utils\Control\ActionUtil;

/**
 * Author:Panda
 * Email:joeyoung0314@qq.com
 * Class AppResult
 * @package App\Utils
 */
class AppResult
{

    /**==============应用返回封装==============**/

    /**
     * 成功返回200
     * @param string $message
     * @param $result
     * @return \Illuminate\Http\JsonResponse
     */
    public static function response200(string $message = 'success', $result = null): \Illuminate\Http\JsonResponse
    {
        return self::response([
            'code'    => 200,
            'message' => $message,
            'result'  => $result
        ]);
    }

    /**
     * 失败返回201
     * @param string $message
     * @param $result
     * @return \Illuminate\Http\JsonResponse
     */
    public static function response201(string $message = 'error', $result = null): \Illuminate\Http\JsonResponse
    {
        return self::response([
            'code'    => 201,
            'message' => $message,
            'result'  => $result
        ]);
    }

    /**
     * 参数缺失返回203
     * @param string $message
     * @param $result
     * @return \Illuminate\Http\JsonResponse
     */
    public static function response203(string $message = 'error', $result = null): \Illuminate\Http\JsonResponse
    {
        return self::response([
            'code'    => 203,
            'message' => $message,
            'result'  => $result
        ]);
    }

    /**
     * 授权无用户返回204
     * @param string $message
     * @param $result
     * @return \Illuminate\Http\JsonResponse
     */
    public static function response204(string $message = '暂无此用户，请先授权手机号，并登录', $result = null): \Illuminate\Http\JsonResponse
    {
        return self::response([
            'code'    => 204,
            'message' => $message,
            'result'  => $result
        ]);
    }

    /**
     * 请求次数过多返回206
     * @param string $message
     * @param $result
     * @return \Illuminate\Http\JsonResponse
     */
    public static function response206(string $message = '请求次数过多', $result = null): \Illuminate\Http\JsonResponse
    {
        return self::response([
            'code'    => 206,
            'message' => $message,
            'result'  => $result
        ]);
    }

    /**
     * 身份信息已过期400
     * @param string $message
     * @param $result
     * @return \Illuminate\Http\JsonResponse
     */
    public static function response400(string $message = 'Identity is overdue', $result = null): \Illuminate\Http\JsonResponse
    {
        return self::response([
            'code'    => 401,
            'message' => $message,
            'result'  => $result
        ]);
    }

    /**
     * 致命错误500
     * @param string $message
     * @param $result
     * @return \Illuminate\Http\JsonResponse
     */
    public static function response500(string $message = 'Fatal error', $result = null): \Illuminate\Http\JsonResponse
    {
        return self::response([
            'code'    => 500,
            'message' => $message,
            'result'  => $result
        ]);
    }

    /**
     * 权限未拥有777
     * @param string $message
     * @param $result
     * @return \Illuminate\Http\JsonResponse
     */
    public static function response777(string $message = 'No Access', $result = null): \Illuminate\Http\JsonResponse
    {
        return self::response([
            'code'    => 777,
            'message' => $message,
            'result'  => $result
        ]);
    }

    /**
     * 返回统一格式
     * @param array $data
     * @return \Illuminate\Http\JsonResponse
     */
    public static function response(array $data): \Illuminate\Http\JsonResponse
    {
        $response = response()->json($data);
        $action = ActionUtil::getInstance()->get();
        if (!empty($action)){
            if (!empty($data['result']) && strlen(json_encode($data['result'])) > 10000){
                $data['result'] = '数据太多，不记录';
            }
            LogUtil::info('响应结果', $action['controller'], $action['action'],$data);
        }
        //记录Admin返回结果
        if(defined('ADMIN_REQUEST_ID')) {
            if (!empty($data['result']) && strlen(json_encode($data['result'])) > 10000){
                $data['result'] = '数据太多，不记录';
            }
            AdminAccessLogModel::query()->where('id',ADMIN_REQUEST_ID)->update(['result'=>json_encode($data)]);
        }
        return $response;
    }
}